package org.nearby.note.dialog;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.HashMap;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

import org.nearby.note.utils.Constant;
import org.nearby.note.utils.Message;
import org.nearby.note.utils.common;
import org.nearby.note.utils.props;

public class ClockInDlg extends JDialog implements ActionListener{
	private JButton clockInButton;
	
	private int winWidth;
	private int winHeight;
	private HashMap<String, Integer> d = null;

	public ClockInDlg(JFrame parent, String title, boolean modal) {
		super(parent, title, modal);
		
		initWin();
		initBind();
	}
	private void initWin(){
		this.setLayout(null);
		this.winWidth = 500;
		this.winHeight = 500;
	    this.setSize(winWidth, winHeight);
	    
	    clockInButton = new JButton("打卡");
	    clockInButton.setBounds(20, 20, 100, 30);
	    clockInButton.setFocusPainted(false);
	    
	    this.add(clockInButton);
	    
	    this.setResizable(false);
		this.setVisible(true);
	}
	
	private void initBind(){
		
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		String command = e.getActionCommand();
		if(command.equals("打卡")){
		    new Thread(new MyThread()).start();
		}
	}
	
	
/*    public static void main(String[] args){ 
    	new ClockInDlg(null, "打卡", false); 
    }*/
    
    class MyThread implements Runnable{
        @Override
        public void run() {
            try {
                Socket clientSocket = new Socket(props.get("server_ip"), Integer.valueOf(props.get("server_port")));
                Message sendMsg = new Message();
                Message recvMsg;
                
                sendMsg.setCommand(Constant.command.SOCKET_CLOCKIN);
                sendMsg.setFromTo(common.getUnique());
                sendMsg.setSendTo(Constant.SOCKET_SERVER);
                sendMsg.setMsgBody(null);
                
                PrintStream ps = new PrintStream(clientSocket.getOutputStream());
                ps.print(sendMsg.Msg2String());
                
                BufferedReader br = new  BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
                String recvStr = br.readLine();
                if(recvStr != null){
                    recvMsg = Message.Str2Message(recvStr);
                    if(recvMsg.getMsgBody().equals(Constant.return_sign.RETURN_SUCCESS)){
                        JOptionPane.showMessageDialog(ClockInDlg.this, "打卡成功");
                        ClockInDlg.this.clockInButton.setEnabled(false);
                    } 
                }
                JOptionPane.showMessageDialog(ClockInDlg.this, "打卡失败");
                
            } catch (UnknownHostException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
